#include "sharefile.h"
#include <QCheckBox>
#include "tcpclient.h"
#include "opewidget.h"

ShareFile::ShareFile(QWidget *parent)
    : QWidget{parent}
{
    m_pSelectAllPB = new QPushButton("全选");
    m_pCancelSelectAllPB= new QPushButton("取消选择");
    m_pOKPB= new QPushButton("确定");
    m_pCancelPB= new QPushButton("取消");
    m_pSA= new QScrollArea;
    m_pFriendW= new QWidget;
    m_pFriendWVBL = new QVBoxLayout(m_pFriendW);
    m_pButtonGroup= new QButtonGroup(m_pFriendW);
    m_pButtonGroup->setExclusive(false);
    //布局
    QHBoxLayout *pTopHBL = new QHBoxLayout;
    pTopHBL->addWidget(m_pSelectAllPB);
    pTopHBL->addWidget(m_pCancelSelectAllPB);
    pTopHBL->addStretch();

    QHBoxLayout *pDownHBL = new QHBoxLayout;
    pDownHBL->addWidget(m_pOKPB);
    pDownHBL->addWidget(m_pCancelPB);

    QVBoxLayout *pMainVBL = new QVBoxLayout;
    pMainVBL->addLayout(pTopHBL);
    pMainVBL->addWidget(m_pSA);
    pMainVBL->addLayout(pDownHBL);
    //m_pFriendW->setMinimumSize(300, 500);
    m_pFriendW->setLayout(m_pFriendWVBL);
    m_pSA->setWidget(m_pFriendW);
    m_pSA->setWidgetResizable(true);

    setLayout(pMainVBL);
    connect(m_pCancelSelectAllPB, SIGNAL(clicked(bool)), this, SLOT(cancelSelect()));
    connect(m_pSelectAllPB, SIGNAL(clicked(bool)), this, SLOT(selectAll()));
    connect(m_pOKPB, SIGNAL(clicked(bool)), this, SLOT(OKShare()));
    connect(m_pCancelPB, SIGNAL(clicked(bool)), this, SLOT(cancelShare()));
}

ShareFile &ShareFile::getInstance()
{
    static ShareFile instance;
    return instance;
}

void ShareFile::updateFriend(QListWidget *pFriendList)
{
    if(NULL == pFriendList){
        return;
    }
    QList<QAbstractButton*> preFriendList = m_pButtonGroup->buttons();
    for (QAbstractButton* btn : preFriendList) {
        m_pFriendWVBL->removeWidget(btn);
        m_pButtonGroup->removeButton(btn);
        delete btn;
    }
    QCheckBox *pCB = NULL;
    for(int i = 0; i < pFriendList->count(); i++)
    {
        pCB = new QCheckBox(pFriendList->item(i)->text());
        qDebug()<<pFriendList->item(i)->text();
        m_pFriendWVBL->addWidget(pCB);
        m_pButtonGroup->addButton(pCB);
    }

}

void ShareFile::cancelSelect()
{
    QList<QAbstractButton*> cbList = m_pButtonGroup->buttons();
    for(int i = 0; i < cbList.size(); i++)
    {
        if(cbList[i]->isChecked())
        {
            cbList[i]->setChecked(false);
        }
    }

}

void ShareFile::selectAll()
{
    QList<QAbstractButton*> cbList = m_pButtonGroup->buttons();
    for(int i = 0; i < cbList.size(); i++)
    {
        if(!cbList[i]->isChecked())
        {
            cbList[i]->setChecked(true);
        }
    }
}

void ShareFile::OKShare()
{
    QString strName = TcpClient::getInstance().getloginName();
    QString strCurPath = TcpClient::getInstance().getCurPath();
    QString  strShareFileName = OpeWidget::getInstance().getBook()->m_strShareFileName;
    QString strPath = strCurPath + "/" + strShareFileName;
    QList<QAbstractButton*> cbList = m_pButtonGroup->buttons();
    int num = 0;
    for(int i = 0; i < cbList.size(); i++)
    {
        if(cbList[i]->isChecked())
        {
            num++;
        }
    }
    PDU *pdu = mkPDU(CADATEMAX*num + strPath.size() + 1);
    pdu->uiMsgType = ENUM_MSG_TYPE_SHARE_FILE_REQUEST;
    sprintf(pdu->caData, "%s %d",strName.toStdString().c_str(),num);
    int x = 0;
    for(int i = 0; i < cbList.size(); i++)
    {
        if(cbList[i]->isChecked())
        {
            memcpy(pdu->caMsg+x*CADATEMAX,QString(cbList[i]->text()).toStdString().c_str(),CADATEMAX);
            x++;
        }
    }
    memcpy(pdu->caMsg+num*CADATEMAX,strPath.toStdString().c_str(),strPath.size());
}
void ShareFile::cancelShare()
{
    hide();
}
